﻿'''eof
name:贷款及其他（准）贷记卡授信额度使用比例
code:IC6_LoanLimitUsedSituation
tableName:
columnName:
groups:个人征信
dependencies:IC_DSJ
type:常用指标
dataSourceType:
eof'''
import sys
reload(sys)
sys.setdefaultencoding('utf-8')

null_type_list = ['', ' ', None, 'null', 'Null', 'NULL', '/', '-', '--']


def getTable(data, tableName, occurTimes):
    '''
        获取表信息
        :param data:表上层的字典对象
        :param tableName:表的名字
        :param occurTimes:表出现的次数，如果是0代表此表可以不出现，如果是1代表此表必须出现
        :return:该表全部数据
        '''
    table = data.get(tableName)
    if occurTimes == 0:
        if table is None:
            return [{}]
        elif isinstance(table, list) is False or len(table) == 0:
            return None
        else:
            return data.get(tableName)
    if occurTimes == 1:
        if len(table) == 0:
            return [{}]
        elif table is None or isinstance(table, list) is False:
            return None
        else:
            return data.get(tableName)


def isfloat(x):
    '''
    判断x为float类型且大于等于0
    :param x:
    :return:
    '''
    try:
        if isinstance(float(x), float) and float(x)>=0:
            if str(x) in ['inf', 'infinity', 'INF', 'INFINITY', 'True']:
                return False
            else:
                return True
        else:
            return False
    except:
        return False

def value(x,y):
     null_type_list = ['', ' ', None, 'null', 'Null', 'NULL', '/', '-', '--']
     if x.has_key(y) and isfloat(x[y])==True and x[y] not in null_type_list:
         return float(x[y])
     else:
         return 0


def IC6_LoanLimitUsedSituation():
    data=IC_DSJ['data']
    null_type_list = ['', ' ', None, 'null', 'Null', 'NULL', '/', '-', '--']
    try:
        table1=getTable(data,"PCO",1)#单张表
        if table1 is None:
            return u"缺失值"
        elif table1==[{}]:
            return u"缺失值"

        else:
            table3=getTable(table1[0],"PC02",0)
            if table3 is None:
                return u"缺失值"
            elif table3==[{}]:
                return u"缺失值"
            else:
                #非循环贷账户汇总信息段-单张
                table4a=getTable(table3[0],"PC02E",0)
                #循环额度下分账户汇总信息段-单张
                table4b=getTable(table3[0],"PC02F",0)
                #循环贷账户汇总信息段-单张
                table4c=getTable(table3[0],"PC02G",0)
                #贷记卡账户汇总信息段-单张
                table4d=getTable(table3[0],"PC02H",0)
                #准贷记卡账户汇总信息段-单张
                table4e=getTable(table3[0],"PC02I",0)
            if table4a[0] in [ None,[{}]] and table4b[0] in [ None,[{}]] and table4c[0] in [ None,[{}]] and table4d[0] in [ None,[{}]] and table4e[0] in [ None,[{}]]:
                return u"缺失值"
            else:
                #Factor1
                FXHDZH_YE= value(table4a[0],'PC02EJ02')
                XHDZH_YE = value(table4b[0],'PC02FJ02')
                XHEDXFZH_YE = value(table4c[0],'PC02GJ02')
                DJKZH_YYED = value(table4d[0],'PC02HJ04')
                ZDJKZH_TZEDU = value(table4e[0],'PC02IJ04')
                factor1 = FXHDZH_YE + XHDZH_YE + XHEDXFZH_YE + DJKZH_YYED + ZDJKZH_TZEDU
                #Factor2
                FXHDZH_ZE= value(table4a[0],'PC02EJ01')
                XHDZH_ZE = value(table4b[0],'PC02FJ01')
                XHEDXFZH_ZE = value(table4c[0],'PC02GJ01')
                DJKZH_ZE = value(table4d[0],'PC02HJ01')
                ZDJKZH_ZE = value(table4e[0],'PC02IJ01')
                factor2  = FXHDZH_ZE + XHDZH_ZE + XHEDXFZH_ZE + DJKZH_ZE + ZDJKZH_ZE
                if factor2 ==0:
                    return u"缺失值"
                else:
                    return round(factor1/factor2)
    except:
        return u"缺失值"

result = IC6_LoanLimitUsedSituation()




